﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Utility.Messaging;
using System.Utility.UI;
using System.Utility.DBMS;

namespace GuessLAB
{
    public partial class FicsalReport : System.Web.UI.Page
    {
        #region grid
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { Bind(); }
        }

        public void Bind()
        {
            grdList.DataSource = System.Utility.DBMS.DBHelper.GetDataTable("GetFiscalReport", null);
            grdList.DataBind();
        }

        protected void grdList_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdList.EditIndex = e.NewEditIndex;
            Bind();
        }


       


        //if (e.Row.RowType == DataControlRowType.DataRow && e.Row.RowState == DataControlRowState.Edit)
        //{
        //    Calendar cal = (Calendar)e.Row.FindControl("Calendar1");
        //    DateTime dtTest = (DateTime)DataBinder.Eval(e.Row.DataItem, "adate").ToString();
        //    cal.SelectedDate = new DateTime(dtTest.Year, dtTest.Month, dtTest.Day);
        //    cal.VisibleDate = cal.SelectedDate;
            
        //}


        protected void grdList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdList.PageIndex = e.NewPageIndex;
            Bind();
        }

        protected void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                GridViewRow row = grdList.Rows[e.RowIndex];
                //factory.DeleteSource(row);
            }
            catch (Exception ex)
            {
                string msgId = MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, msgId);
            }
            Bind();
        }

        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                try
                {

                    DBHelper.Add("DeleteFISCALREPORT", new object[] { 
            e.CommandArgument
            });
                    PageUtility.RegisterClientScriptBlockByMessageId(this, "S00001");
                    PageUtility.Refresh(formtable.Controls);
                }
                catch (Exception ex)
                {
                    string msgId = MessageUtility.LogAndGetMessageCode(ex);
                    PageUtility.RegisterClientScriptBlockByMessageId(this, msgId);
                }
                Bind();
            }
        }


        protected void grdList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdList.EditIndex = -1;
            Bind();
        }

        protected void grdList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {

                GridViewRow row = grdList.Rows[e.RowIndex];

                string reportId = grdList.DataKeys[e.RowIndex].Value.ToString();
                string year = ((TextBox)row.Cells[0].Controls[0]).Text;
                string fiscalnum = ((TextBox)row.Cells[1].Controls[0]).Text;
                string fiscalper = ((TextBox)row.Cells[2].Controls[0]).Text;
                string startdate = ((TextBox)row.FindControl("gdFiscalStart")).Text;
                string enddate = ((TextBox)row.FindControl("gdFiscalEnd")).Text;

                DBHelper.Add("UpateFISCALREPORT", new object[] { 
            year,
            fiscalnum,            
            startdate,
            enddate,
            fiscalper,
            reportId
            });
                PageUtility.RegisterClientScriptBlockByMessageId(this, "U00000");
                PageUtility.Refresh(formtable.Controls);
            }
            catch (Exception ex)
            {
                string msgId = MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, msgId);
            }
            grdList.EditIndex = -1;
            Bind();
        }

        protected void grdList_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            //GridViewRow row = grdList.Rows[e.NewValues];

            //grdList.EditIndex = -1;
            //Bind();

        }
        #endregion


        protected void Save_Click(object sender, System.EventArgs e)
        {


            try
            {

                DBHelper.Add("AddFISCALREPORT", new object[] { 
            txtyear.Text,
            txtfascalnum.Text,
            Request.Form[txtFISCALSTARTDATE.UniqueID],
            Request.Form[txtFISCALENDDATE.UniqueID],
            txtFISCALPERIOD.Text
            });
                PageUtility.RegisterClientScriptBlockByMessageId(this, "S00001");
                PageUtility.Refresh(formtable.Controls);
            }
            catch (Exception ex)
            {
                string msgId = MessageUtility.LogAndGetMessageCode(ex);
                PageUtility.RegisterClientScriptBlockByMessageId(this, msgId);
            }
            Bind();
        }

        protected void Cancel_Click(object sender, System.EventArgs e)
        {
            PageUtility.Refresh(formtable.Controls);
        }
    }
}